package com.dbms;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Blob;
import java.sql.Clob;

public class SQLTool {

    /**
     * 该方法主要用于将Clob数据转换为String
     * 
     * @param clob 为Clob对象
     * @return String 返回转换后的String
     */
	public static String clobToString(Clob clob) {
		StringBuilder buf = new StringBuilder();
		InputStreamReader is = null;
		try {
			if (clob != null) {
				is = new InputStreamReader(clob.getAsciiStream());
				BufferedReader br = new BufferedReader(is);
				int read = 0;
				char[] c = new char[1024];
				while ((read = br.read(c)) > 0) {
					buf.append(c, 0, read);
				}
				c = null;
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buf.toString();
	}
    /**
     * 该方法主要用于将Clob数据转换为char[]
     * 
     * @param clob 为Clob对象
     * @return String 返回转换后的char[]
     */
	public static char[] clobToChars(Clob clob) {
		CharArrayWriter writer=new CharArrayWriter();
		InputStreamReader is = null;
		try {
			if (clob != null) {
				is = new InputStreamReader(clob.getAsciiStream());
				BufferedReader br = new BufferedReader(is);
				int read = 0;
				char[] c = new char[1024];
				while ((read = br.read(c)) > 0) {
					writer.write(c, 0, read);
				}
				c = null;
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return writer.toCharArray();
	}
	/**
	 * 该方法主要用于将Blob数据转换为String
	 * 
	 * @param blob 为Blob对象
	 * @return String 返回转换后的String
	 */
	public static String blobToString(Blob blob){
		StringBuilder buf = new StringBuilder();
		InputStreamReader is = null;
		if(blob!=null){
			try {
				is = new InputStreamReader(blob.getBinaryStream());
				BufferedReader br = new BufferedReader(is);
				int read = 0;
				char[] c = new char[1024];
				while ((read = br.read(c)) > 0) {
					buf.append(c, 0, read);
				}
				c = null;
				is.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return buf.toString();
	}
	/**
	 * 该方法主要用于将Blob数据转换为byte[]
	 * 
	 * @param blob 为Blob对象
	 * @return String 返回转换后的byte[]
	 */
	public static byte[] blobToBytes(Blob blob){
		ByteArrayOutputStream baos=null;
		InputStream is = null;
		if(blob!=null){
			try {
				is = blob.getBinaryStream();
				baos=new ByteArrayOutputStream();
				int read = 0;
				byte[] b = new byte[1024];
				while ((read = is.read(b)) > 0) {
					baos.write(b,0,read);
				}
				b = null;
				is.close();
				return baos.toByteArray();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 根据数据类型获得SQLType
	 * @param obj 数据对象
	 * @return 返回SQL数据类型
	 */
	public static int getObjectType(Object obj){
		int type=0;
		if(obj instanceof java.math.BigDecimal)
			type=java.sql.Types.DECIMAL;
		else if(obj instanceof Character)
			type=java.sql.Types.CHAR;
		else if(obj instanceof String)
			type=java.sql.Types.VARCHAR; 
		else if(obj instanceof java.util.Date)
			type=java.sql.Types.DATE;
		else if(obj instanceof Boolean)
			type=java.sql.Types.BIT;
		else if(obj instanceof Byte)
			type=java.sql.Types.TINYINT;
		else if(obj instanceof Short)
			type=java.sql.Types.SMALLINT;
		else if(obj instanceof Double)
			type=java.sql.Types.DOUBLE;
		else if(obj instanceof Float)
			type=java.sql.Types.FLOAT;
		else if(obj instanceof Integer)
			type=java.sql.Types.INTEGER;
		else if(obj instanceof Long)
			type=java.sql.Types.BIGINT;
		else if(obj instanceof java.sql.Time)
			type=java.sql.Types.TIME;
		else if(obj instanceof java.sql.Timestamp)
			type=java.sql.Types.TIMESTAMP;
		else if(obj instanceof java.sql.Time)
			type=java.sql.Types.TIME;
		else if(obj instanceof byte[])
			type=java.sql.Types.BLOB;
		else if(obj instanceof java.io.InputStream)
			type=java.sql.Types.BLOB;
		else if(obj instanceof java.io.File)
			type=java.sql.Types.BLOB;
		else if(obj instanceof char[])
			type=java.sql.Types.CLOB;
		else if(obj instanceof java.io.Reader)
			type=java.sql.Types.CLOB;
		else
			type=java.sql.Types.OTHER;
		return type;
	}
	/*
    int sqlTypeForObject(Object obj)
    {
        if(obj == null)
            return 0;
        if(!(obj instanceof Datum))
        {
            if(obj instanceof String)
                return fixedString ? 999 : 12;
            if(obj instanceof BigDecimal)
                return 2;
            if(obj instanceof Boolean)
                return -7;
            if(obj instanceof Integer)
                return 4;
            if(obj instanceof Long)
                return -5;
            if(obj instanceof Float)
                return 6;
            if(obj instanceof Double)
                return 8;
            if(obj instanceof byte[])
                return -3;
            if(obj instanceof Short)
                return 5;
            if(obj instanceof Byte)
                return -6;
            if(obj instanceof Date)
                return 91;
            if(obj instanceof Time)
                return 92;
            if(obj instanceof Timestamp)
                return 93;
            if(obj instanceof SQLData)
                return 2002;
            if(obj instanceof ObjectData)
                return 2002;
        } else
        {
            if(obj instanceof BLOB)
                return 2004;
            if(obj instanceof CLOB)
                return 2005;
            if(obj instanceof BFILE)
                return -13;
            if(obj instanceof ROWID)
                return -8;
            if(obj instanceof NUMBER)
                return 2;
            if(obj instanceof DATE)
                return 91;
            if(obj instanceof TIMESTAMP)
                return 93;
            if(obj instanceof TIMESTAMPTZ)
                return -101;
            if(obj instanceof TIMESTAMPLTZ)
                return -102;
            if(obj instanceof REF)
                return 2006;
            if(obj instanceof CHAR)
                return 1;
            if(obj instanceof RAW)
                return -2;
            if(obj instanceof ARRAY)
                return 2003;
            if(obj instanceof STRUCT)
                return 2002;
            if(obj instanceof OPAQUE)
                return 2007;
            if(obj instanceof INTERVALYM)
                return -103;
        }
        return 1111;
    }*/
}
